<?php 

function checkFid($fid)
{
    $fids = getFid();

    if(!in_array($fid,$fids))
    {
        $fid = '';
        return false;
    }

    return true;
}

function wrapFid($fid)
{
    $fids = getFid();

    if(!in_array($fid,$fids))
    {
        $fid = '';
    }

    return true;
}

function getFid()
{
    return  array('打篮球'=>1,'打羽毛球'=>2,'打网球'=>3,'踢足球'=>4);
}

function getCity()
{
    $CI  = & get_instance();
    $city = $CI->input->cookie('city');

    if(!$city)
    {
        $city = _getCityByNet();

        $city !== false && $CI->input->set_cookie(array(
            'name'   => 'city',
            'value'  => $city,
            'expire' => 86400*3,
            'path'   => '/',
        ));
    }

    return $city;
}

function _getCityByNet()
{
    $CI  = & get_instance();
    $ip  = $CI->input->ip_address();
    //$ip  = '58.211.116.10';

    $url     = 'http://api.map.baidu.com/location/ip?ak='.BAIDU_AK.'&ip='.$ip;
    $opts    = array('http'=>array('method'=>"GET",'timeout'=>1));
    $context = stream_context_create($opts);

    try
    {
        $res     = json_decode(file_get_contents($url,false,$context),true);

        if($res && isset($res['content']['address_detail']['city']))
        {
            if (!empty($res['content']['address_detail']['city']))
            {
                return $res['content']['address_detail']['city'];
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    catch (Exception $e) 
    {
        return false;
    }
}

function error_page($msg='',$url='')
{
    $msg = $msg ? $msg : PARAM_ERROR;
    $url = $url ? $url : site_url();

    $CI  = & get_instance();

    $CI->load->view('common/error',array('msg'=>$msg,'url'=>$url));
    exit($CI->output->get_output());
}

function getUUID() 
{
    $charid = md5 ( uniqid ( mt_rand (), true ) );
    $uuid = substr ( $charid, 0, 8 )  . substr ( $charid, 8, 4 )  . substr ( $charid, 12, 4 )  . substr ( $charid, 16, 4 )  . substr ( $charid, 20, 12 );
    return $uuid;
}   

function getAgeByBirthday($mydate)
{ 
    $birth=$mydate; 
    list($by,$bm,$bd)=explode('-',$birth); 
    $cm=date('n'); 
    $cd=date('j'); 
    $age=date('Y')-$by-1; 
    if ($cm>$bm || $cm==$bm && $cd>$bd) $age++; 
    return $age; 
} 

function checkIE()
{
    $user_agent = strtoupper($_SERVER["HTTP_USER_AGENT"]);

    if(strpos($user_agent,"MSIE 6.0") !== false || strpos($user_agent,"MSIE 7.0") !== false || strpos($user_agent,"MSIE 8.0") !== false)
    {
        return true;
    }
    else
    {
        return false;
    }
}

function isMobile() 
{
  //判断手机发送的客户端标志
  if(isset($_SERVER['HTTP_USER_AGENT'])) {
    $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
    $clientkeywords = array(
      'nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp', 'sie-'
      ,'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod','ipad', 'blackberry', 'meizu',
      'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini',
      'operamobi', 'opera mobi', 'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile'
    );
    // 从HTTP_USER_AGENT中查找手机浏览器的关键字
    if(preg_match("/(".implode('|',$clientkeywords).")/i",$userAgent))
    {
      return 1;
    }
  }
  return 0;
}
